// pages/order-detail/order-detail.ts
import { MerchantAPI } from '../../utils/api'
import type { UserLatestOrder } from '../../types/order-detail'

interface OrderDetailPageData {
  loading: boolean;
  orderInfo: UserLatestOrder | null;
  statusColors: Record<number, string>;
  statusBgColors: Record<number, string>;
}

Page({
  data: {
    loading: false,
    orderInfo: null,
    // 订单状态颜色映射
    statusColors: {
      0: '#ff9800',  // 待处理 - 橙色
      1: '#2196f3',  // 已接单 - 蓝色
      2: '#9c27b0',  // 制作中 - 紫色
      3: '#4caf50',  // 待取餐 - 绿色
      4: '#00bcd4',  // 配送中 - 青色
      5: '#4caf50',  // 已完成 - 绿色
      6: '#9e9e9e'   // 已取消 - 灰色
    },
    statusBgColors: {
      0: '#fff3e0',
      1: '#e3f2fd',
      2: '#f3e5f5',
      3: '#e8f5e9',
      4: '#e0f7fa',
      5: '#e8f5e9',
      6: '#f5f5f5'
    }
  } as OrderDetailPageData,

  onLoad(options: any) {
    const sessionId = options.sessionId;
    if (!sessionId) {
      wx.showToast({
        title: '会话ID不存在',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
      return;
    }

    this.loadOrderDetail(sessionId);
  },

  /**
   * 加载订单详情
   */
  async loadOrderDetail(sessionId: string) {
    this.setData({ loading: true });

    try {
      const response = await MerchantAPI.getUserLatestOrder(sessionId);
      
      if (response.code === 200 && response.data) {
        this.setData({
          orderInfo: response.data
        });
      } else {
        wx.showModal({
          title: '提示',
          content: response.msg || '该用户暂无订单记录',
          showCancel: false,
          success: () => {
            wx.navigateBack();
          }
        });
      }
    } catch (error: any) {
      console.error('加载订单详情失败:', error);
      wx.showModal({
        title: '加载失败',
        content: error?.msg || '网络错误，请稍后重试',
        showCancel: false,
        success: () => {
          wx.navigateBack();
        }
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  /**
   * 拨打电话
   */
  makePhoneCall() {
    const orderInfo = this.data.orderInfo;
    if (!orderInfo || !orderInfo.addressSnapshot) {
      return;
    }

    // 从地址快照中提取电话号码
    const phoneMatch = orderInfo.addressSnapshot.match(/\d{11}/);
    if (phoneMatch) {
      wx.makePhoneCall({
        phoneNumber: phoneMatch[0]
      });
    } else {
      wx.showToast({
        title: '未找到联系电话',
        icon: 'none'
      });
    }
  },

  /**
   * 复制订单号
   */
  copyOrderNo() {
    const orderInfo = this.data.orderInfo;
    if (!orderInfo) return;

    wx.setClipboardData({
      data: orderInfo.orderNo,
      success: () => {
        wx.showToast({
          title: '订单号已复制',
          icon: 'success'
        });
      }
    });
  },

  /**
   * 复制取餐码
   */
  copyPickupCode() {
    const orderInfo = this.data.orderInfo;
    if (!orderInfo || !orderInfo.pickupCode) return;

    wx.setClipboardData({
      data: orderInfo.pickupCode,
      success: () => {
        wx.showToast({
          title: '取餐码已复制',
          icon: 'success'
        });
      }
    });
  },

  /**
   * 刷新订单
   */
  onPullDownRefresh() {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const options = currentPage.options as any;
    
    if (options.sessionId) {
      this.loadOrderDetail(options.sessionId);
    }
    
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  }
});
